﻿// ***********************************************************************
// Assembly         : ResourceOptimizer
// Author           : ray
// Created          : 12-31-2014
//
// Last Modified By : ray
// Last Modified On : 12-31-2014
// ***********************************************************************
// <copyright file="MainForm.cs" company="radray">
//     Copyright (c) radray. All rights reserved.
// </copyright>
// <summary></summary>
// ***********************************************************************

using System;
using System.Text;
using System.Windows.Forms;
using ResourceOptimizeLibrary;

/// <summary>
/// The ResourceOptimizer namespace.
/// </summary>
namespace ResourceOptimizer
{
    /// <summary>
    /// Class MainForm.
    /// </summary>
    public partial class MainForm : Form
    {
        /// <summary>
        /// The CSS optimizer
        /// </summary>
        private CssOptimize cssOptimizer = null;
        /// <summary>
        /// The js optimizer
        /// </summary>
        private JsOptimize jsOptimizer = null;
        /// <summary>
        /// The optimize index
        /// </summary>
        private int optimizeIndex = -1;

        /// <summary>
        /// Initializes a new instance of the <see cref="MainForm"/> class.
        /// </summary>
        public MainForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Handles the Click event of the btnCssOptimize control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void btnCssOptimize_Click(object sender, EventArgs e)
        {
            var fileDialog = new OpenFileDialog();
            fileDialog.Multiselect = true;
            fileDialog.Title = "请选择css文件";
            fileDialog.Filter = "所有文件(*.css)|*.css";
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    var filePaths = fileDialog.FileNames;
                    MessageBox.Show("已选择文件:" + filePaths.Length + "个css文件", "选择文件提示", MessageBoxButtons.OK,
                        MessageBoxIcon.Information);
                    var optimizedCss = cssOptimizer.Optimize(filePaths);
                    rtbCodeEditor.Text = optimizedCss;
                    optimizeIndex = 1;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    optimizeIndex = -1;
                }
            }
            else
            {
                optimizeIndex = -1;
            }
        }

        /// <summary>
        /// Handles the Click event of the btnJsOptimize control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void btnJsOptimize_Click(object sender, EventArgs e)
        {
            var fileDialog = new OpenFileDialog();
            fileDialog.Multiselect = true;
            fileDialog.Title = "请选择js文件";
            fileDialog.Filter = "所有文件(*.js)|*.js";
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    var filePaths = fileDialog.FileNames;
                    MessageBox.Show("已选择文件:" + filePaths.Length + "个js文件", "选择文件提示", MessageBoxButtons.OK,
                        MessageBoxIcon.Information);
                    var optimizedJs = cssOptimizer.Optimize(filePaths);
                    rtbCodeEditor.Text = optimizedJs;
                    optimizeIndex = 2;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    optimizeIndex = -1;
                }
            }
            else
            {
                optimizeIndex = -1;
            }
        }

        /// <summary>
        /// Handles the Load event of the MainForm control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void MainForm_Load(object sender, EventArgs e)
        {
            jsOptimizer = new JsOptimize();
            cssOptimizer = new CssOptimize();
        }

        /// <summary>
        /// Handles the Click event of the CopyToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void CopyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            rtbCodeEditor.Copy();
            MessageBox.Show("已复制");
        }

        /// <summary>
        /// Handles the Click event of the WriteToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void WriteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (optimizeIndex == 1 || optimizeIndex == 2)
            {
                var dialog = new FolderBrowserDialog();
                dialog.Description = "请选择文件路径";
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        var folderPath = dialog.SelectedPath;
                        var extension = optimizeIndex == 1 ? "css" : "js";
                        var filePath = folderPath + "/" + "optimize." + extension;
                        if (optimizeIndex == 1)
                        {
                            cssOptimizer.WriteOut(filePath, rtbCodeEditor.Text, Encoding.UTF8);
                        }
                        else if (optimizeIndex == 2)
                        {
                            jsOptimizer.WriteOut(filePath, rtbCodeEditor.Text, Encoding.UTF8);
                        }
                        MessageBox.Show("已写出文件至" + filePath);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
            }
            else
            {
                MessageBox.Show("请先选择要优化的代码");
            }
        }
    }
}